package com.neu.service;

import com.github.pagehelper.PageHelper;
import com.neu.dao.ShoppingUserMapper;
import com.neu.domain.ShoppingOrderExample;
import com.neu.domain.ShoppingUser;
import com.neu.domain.ShoppingUserExample;
import com.neu.vo.UserVo;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingUserService {
	@Resource
	private ShoppingUserMapper shoppingUserMapper;
	@GetMapping("/findUser")
	public ShoppingUser findById(Integer userId) {
		// TODO 通过id来查找用户。
        return shoppingUserMapper.selectByPrimaryKey(userId);
	}
	public int count() {
    	ShoppingUserExample example = new ShoppingUserExample();
    	example.or().andDeletedEqualTo(false);
		return (int) shoppingUserMapper.countByExample(example);	
	}
	 public UserVo findUserVoById(Integer userId) {
	    ShoppingUser user = findById(userId);
	    UserVo userVo = new UserVo();
	    userVo.setNickname(user.getNickname());
	    userVo.setAvatar(user.getAvatar());
	    return userVo;
	}
   
	public List<ShoppingUser> selectByExample(Integer limit,Integer page,String mobile,String order,String sort,String username) {
		ShoppingUserExample example = new ShoppingUserExample();
		ShoppingUserExample.Criteria criteria = example.createCriteria();
		if (!StringUtils.isEmpty(username)) {
			criteria.andUsernameLike("%" + username + "%");
		}
		if (!StringUtils.isEmpty(mobile)) {
			criteria.andMobileEqualTo(mobile);
			}
		criteria.andDeletedEqualTo(false);
		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			example.setOrderByClause(sort +" "+ order);
			}
		PageHelper.startPage(page, limit);
		return shoppingUserMapper.selectByExample(example);
		}
    public List<ShoppingUser> querySelective(String username, String mobile, Integer page, Integer size, String sort, String order) {
        ShoppingUserExample example = new ShoppingUserExample();
        ShoppingUserExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return shoppingUserMapper.selectByExample(example);
    }
}

